package pro.cherish.knowledgeormmysql.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pro.cherish.knowledgeormmysql.entity.Teacher;
import pro.cherish.knowledgeormmysql.mapper.TeacherMapper;
import pro.cherish.knowledgeormmysql.service.TeacherService;
import pro.cherish.knowledgeutil.date.DateUtilExt;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 创建人：cherish
 * @version 版本号：V1.0
 * <p>
 * ***************************修订记录************************************
 * 2019-11-08 cherish 创建该类功能。
 * **********************************************************************
 * </p>
 * @ClassName 类名：TeacherController
 * @Description 功能说明：
 * <p>
 * TODO
 * </p>
 * **********************************************************************
 * @date 创建日期： 2019-11-08 cherish
 */
@RestController
@Slf4j
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @RequestMapping("/teacher/select")
    public Object selectTeacher(){
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        List<Teacher> list = teacherService.list(wrapper);
        return  list;
    }

    @RequestMapping("/teacher/del")
    public Object del(Integer id){
        boolean result = teacherService.removeById(id);
        return  result;
    }

    @RequestMapping("/teacher/insert")
    public Object insert(String name,Integer age){
        Teacher teacher = Teacher.builder().id(IdUtil.randomUUID()).name(name).age(age).version(1L).build();
        boolean result = teacherService.save(teacher);
        return  result;
    }

    @RequestMapping("/teacher/upd")
    public Object upd(String id){
        Teacher oldTeacher = teacherService.getById(id);
        Teacher teacher = Teacher.builder().id(id).name("update").age(0).version(oldTeacher.getVersion()).build();
        boolean result = teacherService.updateById(teacher);
        return  result;
    }


    @Autowired
    private TeacherMapper teacherMapper ;

    @RequestMapping("/teacher/query")
    public Object query(String id){
       return teacherMapper.query();
    }

}
